﻿#pragma once
#include <atomic>
#include <memory>
#include "zyEnvironment.h"


namespace BlackMoonUI {
	class zyObject {
    private:
        LPVOID m_pSelf;
        std::atomic_size_t m_refCount;

        FnWndProc m_fnWndProc;
        FnChildWndProc m_fnChildWndProc;
        FnDestroy m_fnDestroy;

        // 使用 unique_ptr 延迟初始化容器
        std::unique_ptr<std::unordered_map<std::wstring, LPVOID>> m_pPtrData;
        std::unique_ptr<std::unordered_map<std::wstring, std::wstring>> m_pStringData;
        std::unique_ptr<std::unordered_map<std::wstring, TRegEvent*>> m_pRegEvents;

        void releaseEventMap(); // 私有函数声明

    public:
        // 禁用拷贝和移动
        zyObject(const zyObject&) = delete;
        zyObject& operator=(const zyObject&) = delete;
        zyObject(zyObject&&) = delete;
        zyObject& operator=(zyObject&&) = delete;

        // 静态工厂方法
        static zyObject* Create();

        // 构造与析构
        zyObject();
        ~zyObject();

        // 窗口消息处理
        void setWndProc(FnWndProc fnWndProc);
        void setChildWndProc(FnChildWndProc fnChildWndProc);
        void setDestroy(FnDestroy fnDestroy);

        LRESULT callWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandle);
        LRESULT callChildWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandle);
        void callDestroy();

        // 引用计数
        size_t getRefCount() const;
        size_t addRef();
        size_t release();

        // 数据存储
        bool hasPtrData(const std::wstring& key) const;
        void setPtrData(const std::wstring& key, void* data);
        void* getPtrData(const std::wstring& key) const;
        void removePtrData(const std::wstring& key);

        void setStringData(const std::wstring& key, const std::wstring& data);
        std::wstring getStringData(const std::wstring& key) const;
        void removeStringData(const std::wstring& key);

        // 事件注册
        TRegEvent* getRegEvent(const std::wstring& key) const;
        void setRegEvent(const std::wstring& key, TRegEvent* pRegEvent);

        // 自检
        BOOL checkSelf() const;
	};
}
